package com.jiang.controller;

import com.alibaba.fastjson.JSON;
import com.jiang.bean.QueryInfo;
import com.jiang.bean.Teacher;
import com.jiang.service.TeaService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
public class TeaController {

    @Resource
    private TeaService teaService;

    @RequestMapping("/allTea")
    public String getTeaList(QueryInfo queryInfo){
        int numbers = teaService.getTeaCount("%" + queryInfo.getQuery() + "%");
        int pageStart = (queryInfo.getPageNum()-1)*queryInfo.getPageSize();
        List<Teacher> Teas = teaService.getAllTea("%" + queryInfo.getQuery() + "%", pageStart, queryInfo.getPageSize());
        HashMap<String, Object> res = new HashMap<>();
        res.put("numbers",numbers);
        res.put("data",Teas);
        System.out.println("总条数："+numbers);
        String s = JSON.toJSONString(res);
        return s;
    }

    @RequestMapping("/addTea")
    public String addTea(@RequestBody Teacher teacher){
        int i = teaService.addTea(teacher);
        String str = i >0 ? "success" : "error";
        return str;
    }

    @RequestMapping("/deleteTea")
    public String delTea(int id){
        int i = teaService.deleteTea(id);
        String str = i >0 ? "success" : "error";
        return str;
    }

    @RequestMapping("/updateTea")
    public String updataTea(@RequestBody Teacher teacher){
        int i = teaService.updataTea(teacher);
        String str = i >0 ? "success" : "error";
        return str;
    }

    @RequestMapping("/getupdateTea")
    public String getupdateTea(int id){
        Teacher updataTea = teaService.getUpdataTea(id);
        String s = JSON.toJSONString(updataTea);
        return s;
    }

    @RequestMapping("/selectBynum")
    public String selectnum(String numno){
        Teacher teacher = teaService.selectBynum(numno);
        String s = JSON.toJSONString(teacher);
        return s;
    }

}
